﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EAL.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace EAL.Database
{
    //public class PositionList : BaseList<Position, DbContext>
    //{
    //    /// <summary>
    //    /// ctor receiving a list of items as data source and the data context the item list
    //    /// was retrieved from
    //    /// </summary>
    //    /// <param name="customers">list of item used as data source</param>
    //    /// <param name="context">data context the item list was retrieved from</param>
    //    public PositionList(IEnumerable<Position> items, DbContext context)
    //        : base(items, context)
    //    {
    //    }
    //}


    public class PositionList : ObservableCollection<Position>
    {
        /// <summary>
        /// empty ctor
        /// </summary>
        public PositionList()
            : base()
        {
        }

        /// <summary>
        /// ctor receiving a list of positions as data source
        /// </summary>
        /// <param name="customers">list of positions used as data source</param>
        public PositionList(IEnumerable<Position> positions)
            : base(positions)
        {
            //subscribe to property changed event of each position in the list
            foreach (Position item in positions)
                item.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(item_PropertyChanged);
        }


        /// <summary>
        /// overridden insert method to add a new position to the list and the underlying data context
        /// </summary>
        /// <param name="index">index to insert the item into the list</param>
        /// <param name="item">item to insert</param>
        protected override void InsertItem(int index, Position item)
        {
            //add item to list itself
            base.InsertItem(index, item);

            //subcribe to property changed event of the newly added item
            item.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(item_PropertyChanged);
        }

        /// <summary>
        /// removes an item from the collection and the data context
        /// </summary>
        /// <param name="index">index of the item to remove</param>
        protected override void RemoveItem(int index)
        {
            //unsubscribe from property changed event of that item ...
            this.Items[index].PropertyChanged -= item_PropertyChanged;

            //... and remove it from collection itself
            base.RemoveItem(index);
        }


        /// <summary>
        /// called when a property of a containing position was changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //call simply base method
            //(the logic exists already, but have to be called manually)
            this.OnPropertyChanged(e);
        }
    }
}
